using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Member_Shop_Message : Member, IRequiresSessionState
{
	protected ScriptManager ScriptManager1;
	protected HiddenField hfRendOrSendFromName;
	protected HtmlAnchor Pic_R;
	protected HtmlAnchor Pic_S;
	protected GridView GridView_R;
	protected GridView GridView_S;
	protected Panel Panel1;
	protected Literal MessageTitle;
	protected TextBox TxtReceiver;
	protected RequiredFieldValidator RequiredFieldValidator1;
	protected TextBox TxtTitle;
	protected Label LbTxtError;
	protected RequiredFieldValidator RequiredFieldValidator2;
	protected TextBox TxtContent;
	protected Label LbComtentError;
	protected RequiredFieldValidator RequiredFieldValidator3;
	protected CheckBox CheckBox1;
	protected Button ButdSave;
	protected PlaceHolder SendPanel;
	protected Literal Literal1;
	protected Literal LiSender;
	protected Literal LiReceiver;
	protected Literal TextReceiver;
	protected Literal TextTitle;
	protected TextBox TextContent;
	protected Button Button1;
	protected Button ButtonReply;
	protected PlaceHolder ReadPanel;
	protected Panel Panel0;
	private UserBll _user;
	private MessageBll _Msg;
	public string m_flag = "0";
	private int MessageId = -1;
	private string Sender = string.Empty;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected UserBll m_user
	{
		get
		{
			UserBll user;
			if (this._user == null)
			{
				this._user = new UserBll(this.conn);
				user = this._user;
			}
			else
			{
				user = this._user;
			}
			return user;
		}
	}
	protected MessageBll m_Msg
	{
		get
		{
			MessageBll msg;
			if (this._Msg == null)
			{
				this._Msg = new MessageBll(this.conn);
				msg = this._Msg;
			}
			else
			{
				msg = this._Msg;
			}
			return msg;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (base.IsPostBack)
		{
			if (Fetch.Get("action") == "read")
			{
				this.m_Msg.SetMessageIsRead(Convert.ToInt32(Fetch.Get("MessageID")));
				this.BindList();
			}
		}
		if (Fetch.Get("action") == "send")
		{
			this.ReadSend_Flag();
			if (!string.IsNullOrEmpty(Fetch.Get("toUser")))
			{
				this.m_Msg.SetMessageIsRead(Convert.ToInt32(Fetch.Get("MessageID")));
				this.TxtReceiver.Text = base.Server.UrlDecode(Fetch.Get("toUser"));
				this.BindList();
			}
			if (Fetch.GetID("MessageID") > 0)
			{
				this.m_Msg.SetMessageIsRead(Convert.ToInt32(Fetch.Get("MessageID")));
				this.MessageId = Fetch.GetID("MessageID");
				if (!base.IsPostBack)
				{
					this.BandMessage(this.MessageId);
					this.BindList();
				}
			}
			this.ReadPanel.Visible = false;
			this.SendPanel.Visible = true;
			this.Panel0.Visible = true;
			this.Panel1.Visible = false;
		}
		else
		{
			if (Fetch.Get("action") == "read")
			{
				this.ReadSend_Flag();
				this.m_Msg.SetMessageIsRead(Convert.ToInt32(Fetch.Get("MessageID")));
				if (Fetch.GetID("MessageID") > 0)
				{
					this.MessageId = Fetch.GetID("MessageID");
					if (!base.IsPostBack)
					{
						this.BandReadMessage(this.MessageId);
					}
				}
				this.ReadPanel.Visible = true;
				this.SendPanel.Visible = false;
				this.Panel0.Visible = true;
				this.Panel1.Visible = false;
			}
			else
			{
				this.BindList();
				this.Panel0.Visible = false;
				this.Panel1.Visible = true;
			}
		}
	}
	private void ReadSend_Flag()
	{
		string text = Fetch.Get("from");
		if (text == "sendfolder")
		{
			this.m_flag = "1";
			this.hfRendOrSendFromName.Value = text;
		}
		else
		{
			if (text == "receivefolder")
			{
				this.m_flag = "0";
				this.hfRendOrSendFromName.Value = text;
			}
		}
	}
	public void ButdBack_Click(object sender, EventArgs e)
	{
		if (Fetch.Get("from") == "sendfolder")
		{
			base.Response.Redirect("Shop_Message.aspx?tag=S");
		}
		else
		{
			if (Fetch.Get("from") == "receivefolder")
			{
				base.Response.Redirect("Shop_Message.aspx?tag=R");
			}
		}
	}
	protected void BindList()
	{
		string userName = base.user.UserName;
		string text = Fetch.Get("tag");
		if (text != null)
		{
			if (text == "R")
			{
				this.m_flag = "0";
				this.GridView_R.DataSource = this.m_Msg.GetReceiverMessage(userName);
				this.GridView_R.DataBind();
				this.GridView_S.Visible = false;
				this.Pic_R.Visible = true;
				this.Pic_S.Visible = false;
				return;
			}
			if (text == "S")
			{
				this.m_flag = "1";
				this.GridView_S.DataSource = this.m_Msg.GetSenderMessage(userName);
				this.GridView_S.DataBind();
				this.GridView_R.Visible = false;
				this.Pic_R.Visible = false;
				this.Pic_S.Visible = true;
				return;
			}
		}
		this.m_flag = "0";
		this.GridView_R.DataSource = this.m_Msg.GetReceiverMessage(userName);
		this.GridView_R.DataBind();
		this.GridView_S.Visible = false;
		this.Pic_R.Visible = true;
		this.Pic_S.Visible = false;
	}
	protected void ButdSave_Click(object sender, EventArgs e)
	{
		this.Sender = base.user.UserName;
		string text = this.TxtReceiver.Text;
		string text2 = "";
		if (this.Sender == text)
		{
			this.WriteErrMsg("", Language.GetMemberMsg("Message/SendSelfErr", new string[0]), "");
		}
		int delS = 1;
		if (this.CheckBox1.Checked)
		{
			delS = 0;
		}
		if (this.TxtTitle.Text.Length > 50)
		{
			this.LbTxtError.Text = "标题过长！";
		}
		else
		{
			if (this.TxtContent.Text.Length > 1500)
			{
				this.LbComtentError.Text = "内容过长！";
			}
			else
			{
				int isSave;
				if (this.CheckBox1.Checked)
				{
					isSave = 1;
				}
				else
				{
					isSave = 0;
				}
				string[] array = text.Trim().Replace("，", ",").Split(new char[]
				{
					','
				}, StringSplitOptions.RemoveEmptyEntries);
				string[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					string text3 = array2[i];
					if (this.m_user.CheckUserExist("UserName", text3))
					{
						this.m_Msg.AddMessage(this.Sender, "Users", this.TxtTitle.Text, this.TxtContent.Text, 0, delS, 1, 0, text3, "", isSave);
					}
					else
					{
						text2 = text2 + text3 + ",";
					}
				}
				if (text2 != "")
				{
					this.WriteErrMsg("", Language.GetMemberMsg("Message/SendNotUser", new string[0]), "");
				}
				else
				{
					this.WriteSuccessMsg("", Language.GetMemberMsg("Message/SendSuccess", new string[0]), "Shop_Message.aspx");
				}
			}
		}
	}
	protected void ReSend_OnClick(object sender, EventArgs e)
	{
		base.Response.Redirect("Shop_Message.aspx?action=send&MessageID=" + this.MessageId);
	}
	private void BandMessage(int MsgID)
	{
		DataTable messageByMessageID = this.m_Msg.GetMessageByMessageID(MsgID);
		if (messageByMessageID.Rows.Count == 1)
		{
			if (base.user.UserName != messageByMessageID.Rows[0]["Sender"].ToString() && base.user.UserName != messageByMessageID.Rows[0]["Receiver"].ToString())
			{
				this.WriteErrMsg("", Language.GetMemberMsg("Common/NotData", new string[0]), "");
			}
			if (base.user.UserName == messageByMessageID.Rows[0]["Receiver"].ToString() && messageByMessageID.Rows[0]["IsRead"].ToString() == "0")
			{
				this.m_Msg.SetMessageIsRead(MsgID);
			}
			this.TxtTitle.Text = "回复：" + messageByMessageID.Rows[0]["Title"].ToString();
			this.TxtContent.Text = string.Concat(new string[]
			{
				Environment.NewLine,
				Environment.NewLine,
				"------------------------------------------",
				Environment.NewLine,
				"在【",
				messageByMessageID.Rows[0]["Sender"].ToString(),
				"】短消息中提到：",
				Environment.NewLine,
				Member_Shop_Message.NoHTML(messageByMessageID.Rows[0]["Content"].ToString())
			});
			this.TxtReceiver.Text = messageByMessageID.Rows[0]["Sender"].ToString();
		}
		else
		{
			this.WriteErrMsg("", Language.GetMemberMsg("Common/NotData", new string[0]), "");
		}
	}
	private void BandReadMessage(int MsgID)
	{
		DataTable messageByMessageID = this.m_Msg.GetMessageByMessageID(MsgID);
		if (messageByMessageID.Rows.Count == 1)
		{
			if (base.user.UserName != messageByMessageID.Rows[0]["Sender"].ToString() && base.user.UserName != messageByMessageID.Rows[0]["Receiver"].ToString())
			{
				this.WriteErrMsg("", Language.GetMemberMsg("Common/NotData", new string[0]), "");
			}
			if (base.user.UserName == messageByMessageID.Rows[0]["Receiver"].ToString() && messageByMessageID.Rows[0]["IsRead"].ToString() == "0")
			{
				this.m_Msg.SetMessageIsRead(MsgID);
			}
			if (base.user.UserName == messageByMessageID.Rows[0]["Receiver"].ToString())
			{
				this.TextReceiver.Text = messageByMessageID.Rows[0]["Sender"].ToString();
				this.LiReceiver.Visible = true;
			}
			else
			{
				this.TextReceiver.Text = messageByMessageID.Rows[0]["Receiver"].ToString();
				this.LiSender.Visible = true;
				this.Button1.Visible = false;
			}
			this.TextTitle.Text = messageByMessageID.Rows[0]["Title"].ToString();
			this.TextContent.Text = Member_Shop_Message.NoHTML(messageByMessageID.Rows[0]["Content"].ToString());
		}
		else
		{
			this.WriteErrMsg("", Language.GetMemberMsg("Common/NotData", new string[0]), "");
		}
	}
	public static string NoHTML(string Htmlstring)
	{
		Htmlstring = Regex.Replace(Htmlstring, "<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "<(.[^>]*)>", "", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "([\\r\\n])[\\s]+", "", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "-->", "", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "<!--.*", "", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "&(quot|#34);", "\"", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "&(amp|#38);", "&", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "&(lt|#60);", "<", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "&(gt|#62);", ">", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "&(iexcl|#161);", "¡", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "&(cent|#162);", "¢", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "&(pound|#163);", "£", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "&(copy|#169);", "©", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "&#(\\d+);", "", RegexOptions.IgnoreCase);
		Htmlstring = Regex.Replace(Htmlstring, "{(.[^>]*)}", "", RegexOptions.IgnoreCase);
		Htmlstring.Replace("{", "");
		Htmlstring.Replace("}", "");
		Htmlstring.Replace("<", "");
		Htmlstring.Replace(">", "");
		Htmlstring.Replace("\r\n", "");
		Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
		return Htmlstring;
	}
	protected void DeleteMessage(object sender, CommandEventArgs e)
	{
		if (e.CommandName != "")
		{
			int num = int.Parse(e.CommandName);
			string flag = this.m_flag;
			if (flag != null)
			{
				if (!(flag == "0"))
				{
					if (flag == "1")
					{
						this.m_Msg.SetMessageDelS(num);
					}
				}
				else
				{
					this.m_Msg.SetMessageDelR(num);
				}
			}
			this.BindList();
		}
	}
	protected void GridView_R_PageIndexChanging(object sender, GridViewPageEventArgs e)
	{
		this.GridView_R.PageIndex = e.NewPageIndex;
		this.GridView_R.DataBind();
	}
}
